/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import Connectdata.connectdatabase;
import DTO.Object.OrderDetail.Orderdetails;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.catalina.Session;

/**
 *
 * @author annt60258
 */
public class Order {
    public String productfail="";
    public int numproductfail=0;
public String getidUser(String typeget,String table) 
    {
    int id = 0;
        try {
            connectdatabase conngetid = new connectdatabase();
            
            conngetid.connect();
            conngetid.connectstring("select " + typeget + " from " + table + " order by " + typeget + " DESC ");
            if (conngetid.rs_connect.next()) {
                id = conngetid.rs_connect.getInt(typeget);
                return Integer.toString(id + 1);
            }
conngetid.rs_connect.close();conngetid.s_connect.close();conngetid.con_connect.close();
        } catch (SQLException ex) {
            return Integer.toString(id + 1);
        }
        return Integer.toString(id + 1);
      }
public String getpicturepath(String id)
    {
    String picturePath="";
        try {
            connectdatabase conngetid = new connectdatabase();
            conngetid.connect();
            conngetid.connectstring("select * from Product where proID = " + id);
            if (conngetid.rs_connect.next()) {
                picturePath = conngetid.rs_connect.getString("Image");
            }
            conngetid.rs_connect.close();conngetid.s_connect.close();conngetid.con_connect.close();
        } catch (SQLException ex) {
            Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
        }

        return picturePath;
      }
public boolean checksoluongsp(Orderdetails orderdetails)
    {
        try {
            connectdatabase connect = new connectdatabase();
            connect.connect();
            for(int i=0;i<orderdetails.getOrderdetail().size();i++)
            {
                connect.connectstring("select * from Product where proID="+orderdetails.getOrderdetail().get(i).getProductID());
                while(connect.rs_connect.next())
                {
                    if(connect.rs_connect.getInt("proNumber")<orderdetails.getOrderdetail().get(i).getQuantity())
                    {
                        productfail=connect.rs_connect.getString("proName");
                        numproductfail=connect.rs_connect.getInt("proNumber");
                        return true;
                    }
                }
            }
            connect.rs_connect.close();connect.s_connect.close();connect.con_connect.close();

        } catch (SQLException ex) {
            Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
}
public boolean InsertDatabaseorder(String username,String date) 
    {
        try {
            Connection con = null;
            DBUtil dbutil = new DBUtil();
            
            String sql = "";
            Class.forName(dbutil.getDriverName());
            //String url = "jdbc:sqlserver://ANNT60258-PC:1433;databaseName=eShopping";
            con = DriverManager.getConnection(dbutil.getURL(), dbutil.getUsername(), dbutil.getPassword());
            Statement stmt = con.createStatement();

            sql=("insert into OrderPayment(userName,orderDate,orderStatus) values('" + username + "','" + date + "',0)");
            stmt.execute(sql);
            con.close();
            stmt.close();
            return true;

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
             return false;
        } catch (SQLException ex) {
            Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
}

public void  InsertOrderDetailandUpdateProduct(Orderdetails orderdetails)
    {
        try {
            DBUtil dbutil = new DBUtil();
            int orderid = Integer.parseInt(getidUser("orderID", "OrderPayment"));
            orderid = orderid-1;
            Statement stmt = null;
            Statement stmt1 = null;
            Connection con = null;
            String sql = "";
            Class.forName(dbutil.getDriverName());
            //String url = "jdbc:sqlserver://ANNT60258-PC:1433;databaseName=eShopping";
            con = DriverManager.getConnection(dbutil.getURL(),dbutil.getUsername(),dbutil.getPassword());
            for (int i = 0; i < orderdetails.getOrderdetail().size(); i++) {
                stmt = con.createStatement();
                sql = ("insert into OrderDetail(orderMother,proID,quantity,priceProduct) values (" + orderid + "" + "," + orderdetails.getOrderdetail().get(i).getProductID() + "" + "," + orderdetails.getOrderdetail().get(i).getQuantity() + "" + "," + orderdetails.getOrderdetail().get(i).getPrice() + ")");
                stmt.execute(sql);
                stmt1 = con.createStatement();
                sql = ("update Product set proNumber=proNumber-" + orderdetails.getOrderdetail().get(i).getQuantity() + " where proID=" + orderdetails.getOrderdetail().get(i).getProductID());
                stmt1.execute(sql);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
        }
            
    }
}
